package com.friday.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.friday.dao.DeliveryDao;
import com.friday.dao.OrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DeliveryController {

    @Autowired
    private DeliveryDao deliveryDao;
    @Autowired
    private OrderDao orderDao;

    //确认发货，生成发货单
    @RequestMapping("/deliveryConfirm")
    public JSONObject addDelivery(@RequestParam("operator_id") int operator_id,@RequestParam("order_id") int order_id){
        JSONObject result = new JSONObject();
        //预置错误
        result.put("result","fail");
        result.put("delivery_id",null);
        //获取订单信息并插入到发货单中
        JSONObject delivery = orderDao.getOrderById(order_id);
        delivery.put("operator_id",operator_id);
        delivery.put("order_id",order_id);
        orderDao.changeStatus(order_id,"待收货");
        int tag1 = deliveryDao.addDelivery(delivery);
        if(tag1>0 ){
            int delivery_id = delivery.getInteger("id");
            int tag3=0;
            //获取订单详情并插入到发货单详情中
            JSONArray deliverydtl =orderDao.getOrderDtlByOId(order_id);
            for(int i=0;i<deliverydtl.size();i++){
                deliverydtl.getJSONObject(i).put("delivery_id",delivery_id);
                tag3+=deliveryDao.addDeliveryDtl(deliverydtl.getJSONObject(i));
            }
            if(tag3==deliverydtl.size()){
                result.put("result","success");
                result.put("delivery_id",delivery_id);
            }
        }
        return result;
    }


    @RequestMapping("/getDelivery")
    public JSONObject getDeliveryById(@RequestParam("id") int id){
        return deliveryDao.getDeliveryById(id);
    }

    @RequestMapping("/deliveryList")
    public JSONArray getAllDelivery(){
        return deliveryDao.getAllDelivery();
    }

    @RequestMapping("/deliveryDetail")
    public JSONObject getDeliverydtlById(@RequestParam("id") int id){
        JSONObject result = deliveryDao.getDeliveryById(id);
        result.put("deliverydtl",deliveryDao.getDeliverydtlById(id));
        return result;
    }

    @RequestMapping("/deliveryRecordAdd")
    public String addDelivryRecord(@RequestBody JSONObject dcord){

        int deliverydtl_id =dcord.getInteger("id");
        //删除原来的deliveryRecord
        deliveryDao.delDeliveryRecordByid(deliverydtl_id);
        int goods_id =dcord.getInteger("goods_id");
        JSONArray record=dcord.getJSONArray("deliveryRecord");
        int tag=0;
        for(int i=0;i<record.size();i++){
            record.getJSONObject(i).put("deliverydtl_id",deliverydtl_id);
            record.getJSONObject(i).put("goods_id",goods_id);
            tag+=deliveryDao.addDeliveryRecord(record.getJSONObject(i));
        }
        if(tag==record.size()){
            //修改dtl状态为已拣配
            deliveryDao.changeDtlSta(deliverydtl_id,"已拣配");
            //修改对应delivery状态为已拣配
            deliveryDao.changeDeliBydtl(deliverydtl_id);
            return "success";
        }
        return "fail";
    }

    @RequestMapping("/deliveryRecordList")
    public JSONArray getDeliRecordBydtl(@RequestParam("id") int id){
        //通过dtl_id获取deliveryRecord
        return deliveryDao.getDeliRecordBydtl(id);
    }



    @RequestMapping("/deliveryDelete")
    public String delDelivery(@RequestParam("id") int id){
        int order_id=deliveryDao.getDeliveryById(id).getInteger("order_id");
        //删除delivery，deliverydtl和deliveryrecord
        int tag1 = deliveryDao.delDeliveryById(id);
        JSONArray dtl=deliveryDao.getDeliverydtlById(id);
        for(int i=0;i<dtl.size();i++){
            deliveryDao.delDeliveryRecordByid(dtl.getJSONObject(i).getInteger("id"));
        }
        int tag2 = deliveryDao.delDeliverydtlByDId(id);

        //还原销售单状态
        orderDao.changeStatus(order_id,"待发货");
        if( tag1>0 && tag2>0 ){
            return "success";
        }
        return "fail";
    }

    @RequestMapping("/deliveryReceive")
    public String deliveryReceive(@RequestParam("record_id") int deliRecord_id){
        //给deliveryRecord加上到货日期
        int tag1=deliveryDao.deliRecordReceive(deliRecord_id);
        //将收货完的发货单项状态改为已完成
        int tag2=deliveryDao.checkDeliverydtl(deliRecord_id);
        //将收货完的发货单状态改为已完成，添加完成日期。对应订单改为待付款
        int tag3=deliveryDao.checkDO(deliRecord_id);
        return "success";
    }




    @RequestMapping("/deliveryRecordSend")
    public String deliRecordSend(@RequestParam("id") int deliRecord_id){
        int tag=deliveryDao.deliRecordSend(deliRecord_id);
        return "success";
    }


}
